#Demo of splm package# 

data("Produc", package = "Ecdat");
data("usaww", package = "splm");

library("spdep");
library("splm");

summary(Produc);
fmdemo <- log(gsp) ~ log(pcap) + log(pc) + log(emp) +unemp;
usalw <- mat2listw(usaww);


#Results for the Kapoor et al. (2007)# 
sararremod <- spml(formula = fmdemo, data = Produc, index = NULL, listw = usalw, model = "random", lag = FALSE, spatial.error = "kkp")

#Fixed effect model specified in terms of both spatial effects# 
sararremodfix <- spml(formula = fmdemo, data = Produc, index = NULL, listw = usalw, model = "within", lag = TRUE, spatial.error = "b", effect = "individual", method = "eigen", na.action = na.fail, quiet = TRUE, zero.policy = NULL, interval = NULL, tol.solve = 1e-10, control = list(), legacy = FALSE)

summary(sararremodfix)

#Estimate a model with only individual fixed effects# 
sararremodfix2 <- spml(formula = fmdemo, data = Produc, index = NULL, listw = usalw, model = "within", effect = "individual", method = "eigen", na.action = na.fail, quiet = TRUE, zero.policy = NULL, interval = NULL, tol.solve = 1e-10, control = list(), legacy = FALSE)

summary(sararremodfix2)

eff <- effects(sararremodfix2)
eff

#A spatial error model with time period fixed effects# 
sararremodfix3 <- spml(formula = fmdemo, data = Produc, listw = usalw, model = "within", effect = "time", method = "eigen", na.action = na.fail, quiet = TRUE, zero.policy = NULL, interval = NULL, tol.solve = 1e-10, control = list(), legacy = FALSE)

summary(sararremodfix3)

eff <- effects(sararremodfix3)
eff

#Spatial panel random effects GM mode -- spatial error model#
GM_error <- spgm(formula = fmdemo, data = Produc, listw = usaww, moments = "fullweights", model = "random", spatial.error = TRUE)

summary(GM_error)

#Spatial panel random effects GM mode -- spatial lag & error model#
GM_full <- spgm(formula = fmdemo, data = Produc, listw = usaww, lag = TRUE, moments = "fullweights", model = "random", spatial.error = TRUE)

summary(GM_full)

#Spatial panel fixed effects GM mode -- spatial error model#
GM_error <- spgm(formula = fmdemo, data = Produc, lag = TRUE, listw = usaww, model = "within", spatial.error = TRUE)

summary(GM_error)

#Testing#

#LM  test#
test1 <- bsktest(x = fmdemo, data = Produc, listw = mat2listw(usaww), test = "LM1")
print(class(test1))
test1

test2 <- bsktest(x = fmdemo, data = Produc, listw = mat2listw(usaww), test = "LM2")
test2

#CLM_lambda/CLM_mu test# 

bsktest(x = fmdemo, data = Produc, listw = mat2listw(usaww), test = "CLMmu");

#Spatial Hausman test# 
test1 <- sphtest(x = fmdemo, data = Produc, listw = mat2listw(usaww), spatial.model = "error", method = "GM")
test1

mod1 <- spgm(formula = fmdemo, data = Produc, listw = usaww, lag = TRUE, moments = "fullweights", model = "random", spatial.error = TRUE)

mod2 <- spgm(formula = fmdemo, data = Produc, listw = usaww, lag = TRUE, moments = "fullweights", model = "within", spatial.error = TRUE)

test2 <- sphtest(x = mod1, x2 = mod2)
test2

#Linear hypothesis testing# 
library("lmtest")
coeftest(sararremodfix)









